package com.findmeastore.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.dao.support.DataAccessUtils;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.findmeastore.bo.CityBo;
import com.findmeastore.bo.LocalityBo;
import com.findmeastore.bo.StateBo;
import com.findmeastore.bo.UserCredentialsBo;
import com.findmeastore.criteria.LocalityCriteria;
import com.findmeastore.dao.ILocalityDAO;
import com.findmeastore.pagination.Page;
import com.findmeastore.pagination.PageCriteria;

public class LocalityDAOImpl implements ILocalityDAO{

	private HibernateTemplate hibernateTemplate;

	public void setSessionFactory(SessionFactory sessionFactory){
		this.hibernateTemplate = new HibernateTemplate(sessionFactory);
	}

	public Integer getTotalCityCount(){
		return DataAccessUtils.intResult(hibernateTemplate.find("select count(*) from CityBo"));
	}
	
	public Integer getTotalLocalityCount(){
		return DataAccessUtils.intResult(hibernateTemplate.find("select count(*) from LocalityBo"));
	}

	@SuppressWarnings("unchecked")
	public List<StateBo> getAllStates() {
		return hibernateTemplate.find("from StateBo");
	}

	@SuppressWarnings("unchecked")
	public List<CityBo> getAllCities(){
		return hibernateTemplate.find("from CityBo");
	}
	
	public List<LocalityBo> getAllLocalities(){
		return hibernateTemplate.find("from LocalityBo");
	}
	
	public LocalityBo getLocalityById(Integer id){
		return (LocalityBo)hibernateTemplate.get(LocalityBo.class, id);
	}

	@SuppressWarnings("unchecked")
	public List<CityBo> getCityByStateId(Integer id){
		return hibernateTemplate.find("from CityBo where masterStateId =?",id);
	}

	@SuppressWarnings("unchecked")
	public List<LocalityBo> getAllLocalitiesByCityId(Integer cityId) {
		return hibernateTemplate.find("from LocalityBo where masterCity.id = ?",cityId);
	}
	
	public List<LocalityBo> getLocalitiesByCriteria(LocalityCriteria localityCriteria){
		Criteria crit = hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(LocalityBo.class);
		if(localityCriteria.getIds()!=null&&localityCriteria.getIds().length>0){
			crit.add(Restrictions.in("id", localityCriteria.getIds()));
		}
		if(localityCriteria.getExcludeIds()!=null&&localityCriteria.getExcludeIds().length>0){
			crit.add(Restrictions.not(Restrictions.in("id", localityCriteria.getExcludeIds())));
		}
		if(localityCriteria.getCityId()!=null&&localityCriteria.getCityId()>0){
			crit.add(Restrictions.eq("cityId", localityCriteria.getCityId()));
		}
		return (List<LocalityBo>)crit.list();

	}
	
	public Page getLocalitiesPageByCriteria(LocalityCriteria localityCriteria, PageCriteria pageCriteria){
		Page page = new Page();
		Criteria crit = hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(LocalityBo.class);
		if(localityCriteria.getIds()!=null&&localityCriteria.getIds().length>0){
			crit.add(Restrictions.in("id", localityCriteria.getIds()));
		}
		if(localityCriteria.getExcludeIds()!=null&&localityCriteria.getExcludeIds().length>0){
			crit.add(Restrictions.not(Restrictions.in("id", localityCriteria.getExcludeIds())));
		}
		if(localityCriteria.getCityId()!=null&&localityCriteria.getCityId()>0){
			crit.add(Restrictions.eq("cityId", localityCriteria.getCityId()));
		}
		if(localityCriteria.getNameLike()!=null){
			crit.add(Restrictions.ilike("name", localityCriteria.getNameLike()));
		}
		Integer maxSize = crit.list().size();
		page.setTotalResults(maxSize);
		if(pageCriteria!=null){
			crit.setMaxResults(pageCriteria.getPageSize());
			page.setPageSize(pageCriteria.getPageSize());
			Integer firstResult = pageCriteria.getPageSize()*(pageCriteria.getPageNumber()-1);
			page.setPageNumber(pageCriteria.getPageNumber());
			if(firstResult>maxSize){
				firstResult = 0;
				page.setPageNumber(1);
			}
			crit.setFirstResult(firstResult);
		}
		page.setContents(crit.list());
		return page;

	}
	
	public void saveLocality(LocalityBo localityBo) {
		hibernateTemplate.saveOrUpdate(localityBo);
	}
	
	

}
